﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HWR_Ants.Classes;

namespace HWR_Ants.Forms
{
    public partial class fDrawForm : Form
    {
        SortedList<string,Point> listOfCitys = new SortedList<string,Point>();
        int counter = 1;
        Image drawImage = null;

        public fDrawForm()
        {
            InitializeComponent();
            counter = 1;
        }

        private void fDrawForm_Paint(object sender, PaintEventArgs e)
        {
            Size size = new Size(pictureBox1.Width,pictureBox1.Height);

            drawImage = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                string keyToDelete = String.Empty;
                foreach (KeyValuePair<string, Point> kp in listOfCitys)
                {
                    if (Math.Abs(kp.Value.X - e.X) < 10 &&  Math.Abs(kp.Value.Y - e.Y) < 10)
                    {
                        keyToDelete = kp.Key;
                        break;
                    }
                }
                if (keyToDelete != String.Empty)
                {
                    listOfCitys.Remove(keyToDelete);
                }
            }
            else
            {
                Point newCity = new Point(e.X, e.Y);
                listOfCitys.Add(counter.ToString(),newCity);
                counter++;
            }
            mapUpdate();
        }

        void mapUpdate()
        {
            drawImage = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            int RADIUS = 3;
            using (Graphics g = Graphics.FromImage(drawImage))
            {
                Map sender = new Map(new MapWrapper(listOfCitys));
                using (Pen p = new Pen(Color.FromArgb(26, Color.Blue), 2))
                {
                    for (int i = 0; i < sender.Cities.Count - 1; i++)
                    {
                       for (int j = i + 1; j < sender.Cities.Count; j++)
                       {
                           IList<City> il = sender.Cities;
                           Road road = il.ElementAt(i).Roads(il.ElementAt(j));
                           Point p1 = il.ElementAt(i).Coord;
                           Point p2 = il.ElementAt(j).Coord;
                           g.DrawLine(p, p1, p2);
                       }
                    }
                }
                Pen pen = new Pen(Color.Black, RADIUS / 3);

                IList<Point> ilp = listOfCitys.Values;
                Point[] cp = ilp.ToArray();
                foreach (Point p in cp)
                {
                    g.DrawEllipse(pen, p.X - RADIUS, p.Y - RADIUS, RADIUS * 2, RADIUS * 2);
                    // g.DrawEllipse(Pens.Black ,p.X, p.Y , 5, 5);
                }
            }
            pictureBox1.Image = drawImage;
        }

        private void bt_Save_Click(object sender, EventArgs e)
        {
            List<string> saveFile = new List<string>();
            if (tb_Name.Text == String.Empty)
            {
                MessageBox.Show("Namen eingeben!");
            }
            else if (listOfCitys.Count < 4)
            {
                MessageBox.Show("Mindestens 4 Städte angeben!");
            }
            else
            {
                saveFile.Add("NAME: " + tb_Name.Text);
                saveFile.Add("TYPE: TSP");
                saveFile.Add("COMMENT: " + tb_Comment.Text);
                saveFile.Add("DIMENSION: " + listOfCitys.Count.ToString());
                saveFile.Add("EDGE_WEIGHT_TYPE: EUC_2D");
                saveFile.Add("SELF_CREATED");
                saveFile.Add("NODE_COORD_SECTION");

                foreach (KeyValuePair<string, Point> kp in listOfCitys)
                {
                    saveFile.Add(kp.Key.ToString() + " " + kp.Value.X.ToString() + " " + kp.Value.Y.ToString());
                }

                saveFile.Add("EOF");

                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "TSP-Datein|*.tsp|Alle Dateien|*.*";
                sfd.DefaultExt = ".tsp";
                sfd.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
                sfd.FileName = tb_Name.Text + listOfCitys.Count.ToString();
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    String[] saveArray = saveFile.ToArray();
                    System.IO.File.WriteAllLines(sfd.FileName, saveArray);
                    Tools.Tools.SAVEDTSPFILE = sfd.FileName;
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
        }

        private void bt_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

    }
}
